package com.photoeditor.demo.ui.widget.image;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.photoeditor.R;
import com.photoeditor.demo.app.BaseRecycleViewAdapter;
import com.photoeditor.demo.db.FilterCategoryBean;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.List;

import static com.common.base.ComponentContext.getContext;

/**
 * Created by winn on 17/5/9.
 */

public class FilterCategoryAdapter extends BaseRecycleViewAdapter<FilterCategoryBean> {

    private int mSelItemIndex = 0;
    private WeakReference<View> mCurSelViewBg;
    private boolean mShadowEnable = false;
    private HashMap<String, Integer> mProgressMap;

    //选中效果类型
    public static final int SELECT_EFFECT_CHECK = 1;//打钩
    public static final int SELECT_EFFECT_BG = 2;//背景改变

    private int mSelectEffectType = SELECT_EFFECT_CHECK;

    public void setSelectEffectType(int selectEffectType) {
        mSelectEffectType = selectEffectType;
    }

    private String textColor = null;

    public void setTextColor(String color) {
        textColor = color;
    }

    @Override
    protected RecyclerView.ViewHolder createVHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.item_filtercategorylist_new, parent, false);
        FilterCategoryViewHoler viewHolder = new FilterCategoryViewHoler(layout);
        mProgressMap = new HashMap<>();
        return viewHolder;
    }

    @Override
    protected void showItemView(RecyclerView.ViewHolder holder, int position) {
        FilterCategoryViewHoler viewHoler = (FilterCategoryViewHoler) holder;
        viewHoler.mItemPosition = position;
        FilterCategoryBean info = mData.get(position);
        viewHoler.mCategoryId = info.getCategoryId();
        viewHoler.mCategoryName.setText(info.getName());
        if (mSelItemIndex == position) {
            viewHoler.mCategoryName.setTextColor(getContext().getResources().getColor(R.color.category_text_select));
        } else {
            if (TextUtils.isEmpty(textColor)) {
                viewHoler.mCategoryName.setTextColor(getContext().getResources().getColor(R.color.category_text_normal));
            } else {
                viewHoler.mCategoryName.setTextColor(Color.parseColor(textColor));
            }
        }
    }

    public void setTextShadowEnable(boolean enable) {
        mShadowEnable = enable;
    }

    private void setTextUI(TextView name) {
        if (mShadowEnable) {
            name.setTextColor(name.getResources().getColor(R.color.transparent_bg_text_color));
        } else {
            name.setTextColor(name.getResources().getColor(R.color.white_bg_text_color));
        }
    }

    @Override
    public void setData(@NonNull List<FilterCategoryBean> data) {
        super.setData(data);
        mCurSelViewBg = null;
    }

    public void setSelItemIndex(int selItemIndex, View view) {
        mSelItemIndex = selItemIndex;
        if (mCurSelViewBg != null && mCurSelViewBg.get() != null) {
            mCurSelViewBg.get().setVisibility(View.GONE);
        }
        if (view != null) {
            if (true || mSelectEffectType == SELECT_EFFECT_CHECK) {
                mCurSelViewBg = new WeakReference<>(view.findViewById(R.id.select_bg));
                ((TextView) view.findViewById(R.id.filter_category_name)).setTextColor(view.getContext().getResources().getColor(R.color.category_text_select));
//                view.findViewById(R.id.select_bg).setVisibility(View.VISIBLE);
            } else {
                mCurSelViewBg = new WeakReference<>(view.findViewById(R.id.select_background));
                ((TextView) view.findViewById(R.id.filter_category_name)).setTextColor(view.getContext().getResources().getColor(R.color.category_text_normal));
//                view.findViewById(R.id.select_background).setVisibility(View.VISIBLE);
            }
        }
    }

    public int getSelItemIndex() {
        return mSelItemIndex;
    }

    public void updateProgress(String categoryId, int progress) {
        mProgressMap.put(categoryId, progress);
        notifyDataSetChanged();
    }

    public View getSelItemView() {
        if (mCurSelViewBg != null && mCurSelViewBg.get() != null) {
            return (View) mCurSelViewBg.get().getParent().getParent();
        }
        return null;
    }

    @Override
    public long getAdapterItemId(int position) {
        return position;
    }

    public class FilterCategoryViewHoler extends RecyclerView.ViewHolder {
        public String mCategoryId;
        public int mItemPosition;
        public int mCurTranslateX = 0;
        public TextView mCategoryName;

        public FilterCategoryViewHoler(View itemView) {
            super(itemView);
            mCategoryName = itemView.findViewById(R.id.filter_category_name);
        }
    }

    public int getPositionByCategoryId(String categoryId) {
        int ret = 0;
        if (!TextUtils.isEmpty(categoryId)) {
            for (int i = 0; i < getData().size() - 1; i++) {
                FilterCategoryBean bean = getData().get(i);
                if (categoryId.equals(bean.getCategoryId())) {
                    ret = i;
                }
            }
        }
        return ret;
    }

    public FilterCategoryBean getFilterCategoryBeanByCategoryId(String categoryId) {
        if (!TextUtils.isEmpty(categoryId)) {
            List<FilterCategoryBean> data = getData();
            for (int i = 0; i < data.size() - 1; i++) {
                FilterCategoryBean bean = data.get(i);
                if (categoryId.equals(bean.getCategoryId())) {
                    return bean;
                }
            }
        }
        return null;
    }
}
